<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseAdminController;
use App\Lib\Common;
use Illuminate\Http\Request;
use App\Service\BasicService;
use App\Service\CategoryService;
use App\Service\CodeService;
use App\Service\OrgService;
class BasicController extends BaseAdminController{

    protected $basicService;
    protected $categoryService;
    protected $codeService;
    protected $orgService;
    protected $category;
    protected $org;

    public function _init(){
        $this->basicService = new BasicService();
        $this->categoryService = new CategoryService();
        $this->codeService = new CodeService();
        $this->orgService = new OrgService();
        $this->category = Common::getTree(getData('CategoryDataTag'));
        $this->org = Common::getTree(getData('OrgDataTag'));
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        $search = $request->all();
        $data = $this->basicService->getBasicList(isset($search['limit'])?$search['limit']:15,$search);
        return view("admin.basic.index", ([
            'data'=>$data,
            'category'=>json_encode($this->category),
            'org'=>json_encode($this->org)
        ]));
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request) {
        if($request->isMethod("GET")){
            return view("admin.basic.create",([
                "category"=>json_encode($this->category),
                "org"=>json_encode($this->org )
            ]));
        }else{
            $data = $request->except("_token");
            $this->validateRequest($request,"create");
            $data['site_id'] = getCurrentSiteId();
            $data['price'] = $data['price'] * 100;
            $result = $this->basicService->create($data);
            if($result > 0){
                return $this->success($result,"添加成功");
            }else{
                return $this->error(7000,"添加失败");
            }
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {

    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show() {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request) {
        if($request->isMethod("GET")){
            $id = $request->input('id');
            $data = $this->basicService->find($id);
            return view("admin.basic.update",([
                "category"=>json_encode($this->category),
                "org"=>json_encode($this->org ),
                "data"=>$data
            ]));
        }else{
            $data = $request->except("_token");
            $this->validateRequest($request,"create");
            $data['price'] = $data['price'] * 100;
            $result = $this->basicService->update($data['id'], $data);
            return $this->success($result, "编辑成功");
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request) {
        $id = $request->input('id');
        $count = $this->basicService->checkDel($id);
        if($count > 0){
            return $this->layuiDataError(7000,"请先删除考试批次");
        }
        $result = $this->basicService->delete($id);
        return $this->success($result, "删除成功");
    }

    public function upload(Request $request){
        $file = $request->file('img');
        return uploadImg($file,$request->post("path"));
    }
}
